//
//  MSTabViewController.m
//  MissLove
//
//  Created by KevenTao on 2018/8/26.
//  Copyright © 2018年 KevenTao. All rights reserved.
//

#import "MSTabViewController.h"
#import "MSMeViewController.h"
#import "MSNewViewController.h"
#import "MSEssenceViewController.h"
#import "MSFriendTrendsViewController.h"
#import "MSTabBar.h"
#import "MSNavigationController.h"

@interface MSTabViewController ()

@end

@implementation MSTabViewController

/**
 * 只在初始化的时候设置一次就好
 */
+ (void)initialize
{
    NSMutableDictionary *attrs = [NSMutableDictionary dictionary];
    attrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    attrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    NSMutableDictionary *selectAttrs = [NSMutableDictionary dictionary];
    selectAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:12];
    selectAttrs[NSForegroundColorAttributeName] = [UIColor darkGrayColor];
    
    //通过appearance统一设置所有UITabBarItem的文字属性
    //后面带有UI_APPEARANCE_SELECTOR的方法，都可以通过appearance对象来统一设置
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:attrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectAttrs forState:UIControlStateSelected];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //添加子控制器
    [self setupChildVC:[[MSEssenceViewController alloc] init] title:@"精华" image:@"tabBar_essence_icon" selectedImage:@"tabBar_essence_click_icon"];
    [self setupChildVC:[[MSNewViewController alloc] init]  title:@"新帖" image:@"tabBar_new_icon" selectedImage:@"tabBar_new_click_icon"];
    [self setupChildVC:[[MSFriendTrendsViewController alloc] init]  title:@"关注" image:@"tabBar_friendTrends_icon" selectedImage:@"tabBar_friendTrends_click_icon"];
    [self setupChildVC:[[MSMeViewController alloc] init]  title:@"我" image:@"tabBar_me_icon" selectedImage:@"tabBar_me_click_icon"];
    
    //更换tabBar
//    self.tabBar = [[MSTabBar alloc] init];//tabbar无法直接赋值
    [self setValue:[[MSTabBar alloc] init] forKey:@"tabBar"];//使用kvo赋值
    }

/**
 * 初始化子控制器
 */
- (void)setupChildVC:(UIViewController *)VC title:(NSString *)title image:(NSString *)imageName selectedImage:(NSString *)selectedImageName
{
    VC.navigationItem.title = title;
    //设置文字和图片
    VC.tabBarItem.title = title;
    VC.tabBarItem.image = [UIImage imageNamed:imageName];
    VC.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
//    VC.view.backgroundColor = [UIColor colorWithRed:arc4random_uniform(100)/100.0 green:arc4random_uniform(100)/100.0 blue:arc4random_uniform(100)/100.0 alpha:1.0];
    //包装一个导航控制器，添加导航控制器为tabbarcontroller的子控制器
    MSNavigationController *nav = [[MSNavigationController alloc] initWithRootViewController:VC];
    [self addChildViewController:nav];
}
@end
